<?php
class hook
{
	private $db;
	public $hooks;

	function __construct()
	{
		global $db;
		$this->db = $db;
		$this->hooks = array(
			'after_del_content'	=>	'删除category下的内容后执行的操作，每删除一条，执行一次',
			'manage_content_button'	=>	'内容管理页面头部增加功能性链接',
			'after_admin_links'	=>	'管理后台显示右上角链接之后',
		);
	}

	/**
	 * 执行钩子
	 *
	 * @param string $hookname
	 * @param string $siteid
	 * @param array $params
	 * @param array
	 */
	function op_hook($hookname, $siteid, $params)
	{
		global $db,$current_user;
		if(!isset($this->hooks[$hookname]))
		{
			return true;
		}
		$funcs = $this->get_hook_funcs($hookname,$siteid);
		$opd_func = array();
		if(is_array($funcs))
		{
			foreach ($funcs as $func)
			{
				$plugins = substr(basename($func['plugins']),0,-12);
				if(!file_exists(APP_ROOT . $func['plugins']) || !is_file(APP_ROOT . $func['plugins']))
				{
					continue;
				}
				if(!class_exists($plugins))
				{
					include_once APP_ROOT . $func['plugins'];
				}
				$classname = 'plugins_'.$plugins;
				$funcname = $func['func'];
				if(array_key_exists($classname.'__'.$funcname,$opd_func))
				{
					continue;
				}
				$obj = new $classname;
				if(method_exists($obj,'install')==false)
				{
					continue;
				}
				$obj->install();
				if(!isset($obj->info['funcs'][$funcname]))
				{
					continue;
				}
				$res = $obj->$funcname($params);
				$opd_func[$classname.'__'.$funcname] = $res;
			}
		}
		return $opd_func;
	}

	/**
	 * 获得钩子要执行的方法
	 *
	 * @param string $hook
	 * @param string $siteid
	 * @return false
	 */
	function get_hook_funcs($hook,$siteid)
	{
		global $db;
		$this->db->prepare('select * from ' .DB_PRE.'plugins_hooks where (siteid=? or siteid="") and hook=? and status=?');
		$this->db->set_string($siteid);
		$this->db->set_string($hook);
		$this->db->set_int(STATUS_NORMAL);
		$res = $this->db->execute();
		return $res;
	}


}
?>